package com.ambimmort.shj.service.generalfile;

import cn.net.greenet.common.db_service.BaseDao;
import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Hfdw;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Service;

import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 师梁 on 2015/7/30.
 */
@Service("getWhite2DBService")
public class GetWhite2DBService {
    ResultSet rs = null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public boolean get(String path, String MessageType, String policy_instance, GeneralFileProcess pro) {

        boolean result = false;
        init();
        if (path != null) {
            List<String> list = pro.getContent(path, 1);
            if (!list.isEmpty()) {
//                SessionFactory sf = HibernateUtil.getSessionFactory();
//                Session session = sf.openSession();
//                session.clear();
//                String hql = "from Hfdw";
//                List hfdws = session.createQuery(hql).list();
                Map<String, Integer> map = new HashMap<String, Integer>();
                String sql = GeneralFileProcess.QUERY_APP;
                rs = baseDao.executeSQL("select *from Hfdw", new Object[]{});
                List hfdws = new ArrayList();
                try {
                    if (rs.next()) {
                        hfdws.add(rs);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                if (!hfdws.isEmpty()) {
                    for (int i = 0; i < hfdws.size(); i++) {
                        Hfdw hw = (Hfdw) hfdws.get(i);
                        map.put(hw.getFilter(), hw.getId());
                    }
                }
//                Transaction tx = session.beginTransaction();
                Hfdw hfdw = null;
                try {
                    for (String line : list) {
                        String[] ss = line.split("\t");
                        String url = ss[0];
                        if (!map.containsKey(url)) {
                            hfdw = new Hfdw(url);
//                            session.save(hfdw);
                            int id = map.size() + 1;
                            map.put(url, id);
                        }
                    }
//                    tx.commit();
                    result = true;
                } catch (Exception e) {
//                    tx.rollback();
                    result = false;
                } finally {
//                    session.close();
                    close();
                }
                try {
                    PolicySupport policySupport = PolicySupport.getInstance();
                    String policy = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getContent();
                    String comment = policySupport.getApi().getMessage("0xCF", "默认库", 0).getSvnFile().getComment();
                    policySupport.getApi().updatePolicy("0xCF", "默认库", 0, policy, comment);
                } catch (Exception e) {
                }

            }
        }
        return result;
    }
}
